// pages/jiucanhuibaomonth/jiucanhuibaomonth.js
const app = getApp()
var util = require("../../utils/util.js");
const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mid: null,
    classid: null,
    currentyear: null,
    currentmonth: null,
    year: null,
    month: null,
    active: 0,
    selectdate: util.formatDate(new Date()),
    kidlist: [],
    demo4_days_style: [],
    demo4_days_style_1: [{
      month: 'current',
      day: new Date().getDate(),
      color: '#27c868',
      background: 'white'
    }],
    startdate: util.formatDateMonth(new Date('2000-01-01')),
    enddate: util.formatDateMonth(new Date()),
    yearday: new Date().getFullYear(),
    monthday: new Date().getMonth() + 1,
    day: new Date().getDate(),
    date: util.formatDate(new Date()),
    kidlistyes: [],
    kidlistno: [],
    eatnumno: 0,
    eatnumyes: 0,
    halalfoodnumno: 0,
    halalfoodnumyes: 0,
    name: null,
    qingzhenurl:"../../images/icon_qingzhen.png",
    weifukuanurl:"../../images/icon_weifukuan.png",
    yifukuanurl:"../../images/icon_yifukuan.png",
  },
  next: function (event) {
    console.log(event.detail);
    if (this.data.currentmonth == parseInt(event.detail.currentMonth) && this.data.currentyear == parseInt(event.detail.currentYear)) {
      var style = []
      for (var i = 0; i < this.data.demo4_days_style_1.length; i++) {
        style.push(this.data.demo4_days_style_1[i])
      }
      style.push({
        month: 'current',
        day: 1,
        color: 'black',
        background: 'yellow'
      })
      this.setData({
        yearday: event.detail.currentYear,
        monthday: event.detail.currentMonth,
        demo4_days_style: style,
        day: 1,
        date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
      })
    } else {
      var style = []
      style.push({
        month: 'current',
        day: 1,
        color: 'black',
        background: 'yellow'
      })
      this.setData({
        yearday: event.detail.currentYear,
        monthday: event.detail.currentMonth,
        demo4_days_style: style,
        day: 1,
        date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
      })
    }
    this.queryDataDay();

  },

  prev: function (event) {
    console.log(event.detail);

    if (this.data.currentmonth == parseInt(event.detail.currentMonth) && this.data.currentyear == parseInt(event.detail.currentYear)) {
      var style = []
      for (var i = 0; i < this.data.demo4_days_style_1.length; i++) {
        style.push(this.data.demo4_days_style_1[i])
      }
      style.push({
        month: 'current',
        day: 1,
        color: 'black',
        background: 'yellow'
      })
      this.setData({
        yearday: event.detail.currentYear,
        monthday: event.detail.currentMonth,
        demo4_days_style: style,
        day: 1,
        date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
      })
    } else {

      var style = []
      style.push({
        month: 'current',
        day: 1,
        color: 'black',
        background: 'yellow'
      })

      this.setData({
        yearday: event.detail.currentYear,
        monthday: event.detail.currentMonth,
        demo4_days_style: style,
        day: 1,
        date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
      })
    }

    this.queryDataDay();
  },

  dateChange: function (event) {
    console.log(event.detail);
    if (this.data.currentmonth == parseInt(event.detail.currentMonth) && this.data.currentyear == parseInt(event.detail.currentYear)) {
      var style = []
      for (var i = 0; i < this.data.demo4_days_style_1.length; i++) {
        style.push(this.data.demo4_days_style_1[i])
      }
      style.push({
        month: 'current',
        day: 1,
        color: 'black',
        background: 'yellow'
      })
      this.setData({
        yearday: event.detail.currentYear,
        monthday: event.detail.currentMonth,
        demo4_days_style: this.data.demo4_days_style_1,
        day: style,
        date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
      })
    } else {
      var style = []
      style.push({
        month: 'current',
        day: 1,
        color: 'black',
        background: 'yellow'
      })
      this.setData({
        yearday: event.detail.currentYear,
        monthday: event.detail.currentMonth,
        demo4_days_style: style,
        day: 1,
        date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
      })
    }
    this.queryDataDay();
  },

  dayClick: function (event) {
    console.log(event.detail);


    if (this.data.currentmonth == parseInt(event.detail.month) && this.data.currentyear == parseInt(event.detail.year)) {
      var style = []
      for (var i = 0; i < this.data.demo4_days_style_1.length; i++) {
        style.push(this.data.demo4_days_style_1[i])
      }
      style.push({
        month: 'current',
        day: event.detail.day,
        color: 'black',
        background: 'yellow'
      })
      this.setData({
        yearday: event.detail.year,
        monthday: event.detail.month,
        demo4_days_style: style,
        day: event.detail.day,
        date: util.formatDate(new Date(event.detail.year + "-" + util.formatNumber(event.detail.month) + "-" + util.formatNumber(event.detail.day))),
      })
    } else {

      var style = []
      style.push({
        month: 'current',
        day: event.detail.day,
        color: 'black',
        background: 'yellow'
      })

      this.setData({
        yearday: event.detail.year,
        monthday: event.detail.month,
        demo4_days_style: style,
        day: event.detail.day,
        date: util.formatDate(new Date(event.detail.year + "-" + util.formatNumber(event.detail.month) + "-" + util.formatNumber(event.detail.day))),
      })
    }
    this.queryDataDay();
  },

  queryDataDay: function () {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "classid": that.data.classid,
      "mid": that.data.mid,
      "date": that.data.date,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_mealrecordList"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            kidlistyes: res.data.data.kidlistyes,
            kidlistno: res.data.data.kidlistno,
            eatnumno: res.data.data.eatnumno,
            eatnumyes: res.data.data.eatnumyes,
            halalfoodnumno: res.data.data.halalfoodnumno,
            halalfoodnumyes: res.data.data.halalfoodnumyes,
          })
        }
      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },


  onChange(event) {
    console.log("event================" + event.detail.index)
    if (event.detail.index == 0) {
      this.queryData();
    } else {
      this.queryDataDay();
    }

    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var mid = options.mid
    var classid = options.classid
    var year = options.year
    var month = options.month

    var month1 = parseInt(month)
    if (month1 < 10) {
      month1 = '0' + month1;
    }
    var selectdate = year + "-" + month1
    // var style = this.data.demo4_days_style_1

    var style = []
    style.push({
      month: 'current',
      day: new Date().getDate(),
      color: 'black',
      background: 'yellow'
    })

    this.setData({
      mid: parseInt(mid),
      classid: parseInt(classid),
      name: options.name,
      year: parseInt(year),
      month: parseInt(month),
      currentyear: parseInt(year),
      currentmonth: parseInt(month),
      selectdate: selectdate,
      demo4_days_style: style
    })

    wx.setNavigationBarTitle({
      title: this.data.name + "记录" //页面标题为路由参数
    })
    console.log("mid===" + mid)
    console.log("classid===" + classid)
    console.log("year===" + year)
    console.log("month===" + month)
    this.queryData()
  },


  queryData: function () {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "classid": that.data.classid,
      "mid": that.data.mid,
      "year": that.data.year,
      "month": that.data.month,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_monthsmealreport"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            kidlist: res.data.kidlist,
          })
        }
      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },
  bindDateChange: function (e) {
    console.log(e)
    var selectdate = e.detail.value
    var year = selectdate.substring(0, 4)
    var month = selectdate.substring(5, 7)
    this.setData({
      year: parseInt(year),
      month: parseInt(month),
      selectdate: selectdate
    })
    this.queryData();
  },


  btnPreday: function () {
    var year = this.data.year
    var month = this.data.month

    if (month == 1) {
      month = 12
      year = year - 1
    } else {
      month = month - 1;
    }
    var month1 = month
    if (month1 < 10) {
      month1 = '0' + month1;
    }
    var selectdate = year + "-" + month1
    this.setData({
      year: year,
      month: month,
      selectdate: selectdate
    })
    this.queryData();
  },

  btnNextday: function () {

    var year = this.data.year
    var month = this.data.month
    if (year == this.data.currentyear && month == this.data.currentmonth) {
      wx.showToast({
        title: '还未到达得下个月',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    } else {
      if (month == 12) {
        month = 1
        year = year + 1
      } else {
        month = month + 1;
      }
      var month1 = month
      if (month1 < 10) {
        month1 = '0' + month1;
      }
      var selectdate = year + "-" + month1
      this.setData({
        year: year,
        month: month,
        selectdate: selectdate
      })

    }

    this.queryData();


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  btnyesclick: function (e) {
    var index = e.currentTarget.dataset.index;
    var kidid = this.data.kidlistyes[index].UserID;
    var name = this.data.kidlistyes[index].Name;
    wx.navigateTo({
      url: '../jiucanhuibaokiddetail/jiucanhuibaokiddetail?mid=' + this.data.mid + "&kidid=" + kidid + "&name=" + name
    })

  },

  btnnoclick: function (e) {
    var index = e.currentTarget.dataset.index;
    var kidid = this.data.kidlistno[index].UserID;
    var name = this.data.kidlistno[index].Name;
    wx.navigateTo({
      url: '../jiucanhuibaokiddetail/jiucanhuibaokiddetail?mid=' + this.data.mid + "&kidid=" + kidid + "&name=" + name
    })

  },
})